//导入工具类
const util = require('../../utils/util.js')

// pages/register/register.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headerPath: "/static/image/header.jpeg",
    uploadPath: "",
    email: "",
    username: "",
    password: "",
    nickName: "",
    code: ""
  },

  /**
   * 选择头像
   */
  selectHeader(){
    wx.chooseImage({
      count: 1,
      sourceType: ["album","camera"],
      success: resp => {
        var paths = resp.tempFilePaths;
        this.setData({
          headerPath: paths[0]
        });

        //触发头像的上传
        this.uploadHeader(paths[0]);
      }
    });
  },

  /**
   * 上传头像
   */
  uploadHeader(filePath){
    //上传
    wx.uploadFile({
      url: util.url.uploadPath,
      filePath: filePath,
      name: "file",
      success: resp => {
        this.setData({
          uploadPath: JSON.parse(resp.data).data
        });
      }
    });
  },

  /**
   * 基于邮箱发送验证码
   */
  sendCode(){
    //发送请求
    // wx.request({
    //   url: util.url.sendCode,
    //   data: {
    //     email: this.data.email
    //   },
    //   success: resp => {
    //     wx.showToast({
    //       title: "邮件发送成功！"
    //     });
    //   }
    // });

    util.myajax({
      method: "POST",
      url: util.url.sendCode,
      data: {
        email: this.data.email
      },
      success: resp => {
        wx.showToast({
          title: "邮件发送成功！"
        });
      }
    });

  },

  /**
   * 注册用户
   */
  register(){
    //参数校验

    //发送注册请求
    wx.request({
      method: "POST",
      url: util.url.register,
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        username: this.data.username,
        password: this.data.password,
        nickName: this.data.nickName,
        email: this.data.email,
        headerImg: this.data.uploadPath,
        code: this.data.code
      },
      success: resp => {
        wx.showToast({
          title: '注册成功！',
        })

        //返回上一页
        wx.navigateBack({
          delta: 1
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})